var pageIndex = 1;
var that;
Page({

  /**
   * 页面的初始数据
   */

  data: {
    lists: [],
    loadingTip: '上拉加载更多',
    loadingHidden: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadList(1);
  },
  loadList: function (pageIndex) {
    wx.showLoading({
      title: "加载中...",
    })
    that = this;
    wx.request({
      url: 'https://api.apiopen.top/getTangPoetry',
      data: {
        "page": pageIndex,
        "count": 20
      },
      header: {
        // "content-type": "application/x-www-form-urlencoded"
        "Content-Type": "application/text"
      },
      method: 'POST',
      success: function (res) {
        wx.stopPullDownRefresh();
        console.log(res.data);
        var list = res.data.result;
        var originLists = that.data.lists;
        var newLists = list.length > 0 ? originLists.concat(list) : originLists;
        if (pageIndex == 1) {
          newLists = list;
        } else {

        }
        that.setData({
          lists: newLists,
          loadingHidden: true
        });
        if (list.length = 0) {
          that.setData({
            loadingTip: '没有更多内容'
          });
        }
      },
      fail: function (res) {
        console.log(res);
        wx.stopPullDownRefresh();
      },
      complete: function (res) {
        wx.hideLoading();
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    pageIndex = 1;
    that.setData({
      loadingHidden: false
    });
    this.loadList(1);
    console.log("onPullDownRefresh");
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("onReachBottom");
    that.setData({
      loadingHidden: false
    });
    // 为页数迭加1
    this.loadList(++pageIndex);

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})